﻿using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Localization;
using Xpedite.Resources;
using Xpedite.Resources.Combiners;
using Xpedite.Resources.Definitions;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources.Combiners
{
  [TestClass]
  public class TextCombinerTest
  {
    private readonly Mock<IResourceDefinition> _resourceDefinition = new Mock<IResourceDefinition>(MockBehavior.Strict);
    private readonly Mock<IResourceUrl> _virtualUrl = new Mock<IResourceUrl>(MockBehavior.Strict);

    public TextCombinerTest()
    {
      _resourceDefinition.SetupGet(mock => mock.ResourceType).Returns(ResourceType.Css);
      _resourceDefinition.SetupGet(mock => mock.ReferenceName).Returns("Composite-Resource");
      _virtualUrl.SetupGet(mock => mock.VirtualPath).Returns("http://localhost/combined.xcr");
    }

    [TestMethod]
    public void TestCombineWithNoResourcesDefined()
    {
      _resourceDefinition.SetupGet(mock => mock.Resources).Returns(new IResourceContainer[0]);

      var container = new TextCombiner().Combine(_virtualUrl.Object, _resourceDefinition.Object);
      var resource = container.Resource;

      Assert.AreEqual(ResourceType.Css, container.ResourceType);
      Assert.AreEqual(_virtualUrl.Object, container.ResourceUrl);

      Assert.IsTrue(resource.Compressable);
      Assert.AreEqual(0, resource.Bytes.Length);
      Assert.AreEqual(ResourceType.Css, resource.Type);
      Assert.AreEqual(Encoding.UTF8, resource.Encoding);
      Assert.AreEqual(DateTime.UtcNow.Date, resource.LastModified);
    }

    [TestMethod]
    public void TestCombineWithSingleResourcesDefined()
    {
      _resourceDefinition.SetupGet(mock => mock.Resources).Returns(new [] { 
        BuildResource(ResourceType.Css, Encoding.UTF8, "My Content", DateTime.UtcNow.Date.AddDays(-7), true) 
      });

      var container = new TextCombiner().Combine(_virtualUrl.Object, _resourceDefinition.Object);
      var resource = container.Resource;

      Assert.AreEqual(ResourceType.Css, container.ResourceType);
      Assert.AreEqual(_virtualUrl.Object, container.ResourceUrl);

      Assert.IsTrue(resource.Compressable);
      Assert.AreEqual(ResourceType.Css, resource.Type);
      Assert.AreEqual(Encoding.UTF8, resource.Encoding);
      Assert.AreEqual(DateTime.UtcNow.Date.AddDays(-7), resource.LastModified);
      Assert.AreEqual("My Content" + Environment.NewLine, Encoding.UTF8.GetString(resource.Bytes));
    }

    [TestMethod]
    public void TestCombineWithMultipleLikeResourcesDefined()
    {
      _resourceDefinition.SetupGet(mock => mock.Resources).Returns(new[] { 
        BuildResource(ResourceType.Css, Encoding.UTF8, "My Content 1", DateTime.UtcNow.Date.AddDays(-7), true),
        BuildResource(ResourceType.Css, Encoding.UTF8, "My Content 2", DateTime.UtcNow.Date.AddDays(-14), true) 
      });

      var container = new TextCombiner().Combine(_virtualUrl.Object, _resourceDefinition.Object);
      var resource = container.Resource;

      Assert.AreEqual(ResourceType.Css, container.ResourceType);
      Assert.AreEqual(_virtualUrl.Object, container.ResourceUrl);

      Assert.IsTrue(resource.Compressable);
      Assert.AreEqual(ResourceType.Css, resource.Type);
      Assert.AreEqual(Encoding.UTF8, resource.Encoding);
      Assert.AreEqual(DateTime.UtcNow.Date.AddDays(-14), resource.LastModified);
      Assert.AreEqual("My Content 1" + Environment.NewLine + "My Content 2" + Environment.NewLine, Encoding.UTF8.GetString(resource.Bytes));
    }

    [TestMethod]
    public void TestCombineWithMultipleResourcesDefinedWithDifferingTypes()
    {
      _resourceDefinition.SetupGet(mock => mock.Resources).Returns(new[] { 
        BuildResource(ResourceType.Css, Encoding.UTF8, "My Content 1", DateTime.UtcNow.Date.AddDays(-7), true),
        BuildResource(ResourceType.JavaScript, Encoding.UTF8, "My Content 2", DateTime.UtcNow.Date.AddDays(-14), true) 
      });

      var container = new TextCombiner().Combine(_virtualUrl.Object, _resourceDefinition.Object);

      Expect.Exception(() => container.Resource, new XpediteException(String.Format(ExceptionMessages.InconsistantResourceFound, "Composite-Resource", ResourceType.Css)));
    }

    [TestMethod]
    public void TestCombineWithMultipleResourcesDefinedWithDifferingEncodings()
    {
      _resourceDefinition.SetupGet(mock => mock.Resources).Returns(new[] { 
        BuildResource(ResourceType.Css, Encoding.UTF8, "My Content 1", DateTime.UtcNow.Date.AddDays(-7), true),
        BuildResource(ResourceType.Css, Encoding.Unicode, "My Content 2", DateTime.UtcNow.Date.AddDays(-14), true) 
      });

      var container = new TextCombiner().Combine(_virtualUrl.Object, _resourceDefinition.Object);
      var resource = container.Resource;

      Assert.AreEqual(ResourceType.Css, container.ResourceType);
      Assert.AreEqual(_virtualUrl.Object, container.ResourceUrl);

      Assert.IsTrue(resource.Compressable);
      Assert.AreEqual(ResourceType.Css, resource.Type);
      Assert.AreEqual(Encoding.Unicode, resource.Encoding);
      Assert.AreEqual(DateTime.UtcNow.Date.AddDays(-14), resource.LastModified);
      Assert.AreEqual("My Content 1" + Environment.NewLine + "My Content 2" + Environment.NewLine, Encoding.Unicode.GetString(resource.Bytes));
    }

    [TestMethod]
    public void TestCombineWithMultipleResourcesDefinedWithUncompressableResource()
    {
      _resourceDefinition.SetupGet(mock => mock.Resources).Returns(new[] { 
        BuildResource(ResourceType.Css, Encoding.UTF8, "My Content 1", DateTime.UtcNow.Date.AddDays(-7), true),
        BuildResource(ResourceType.Css, Encoding.UTF8, "My Content 2", DateTime.UtcNow.Date.AddDays(-14), false) 
      });

      var container = new TextCombiner().Combine(_virtualUrl.Object, _resourceDefinition.Object);

      Expect.Exception(() => container.Resource, new XpediteException(String.Format(ExceptionMessages.UncompressibleResourceFound, "Composite-Resource")));
    }

    private static IResourceContainer BuildResource(ResourceType resourceType, Encoding encoding, String content, DateTime lastModified, Boolean compressable)
    {
      var resource = new Mock<IResource>(MockBehavior.Strict);

      resource.SetupGet(mock => mock.Bytes).Returns(encoding.GetBytes(content));
      resource.SetupGet(mock => mock.LastModified).Returns(lastModified);
      resource.SetupGet(mock => mock.Compressable).Returns(compressable);
      resource.SetupGet(mock => mock.Encoding).Returns(encoding);
      resource.SetupGet(mock => mock.Type).Returns(resourceType);

      var container = new Mock<IResourceContainer>(MockBehavior.Strict);

      container.SetupGet(mock => mock.Resource).Returns(resource.Object);
      container.SetupGet(mock => mock.ResourceType).Returns(resourceType);

      return container.Object;
    }
  }
}
